#include "client.h"


int receive_message(int socket, Message *msg) {
    int type, length;

    // 接收消息类型
    if (read(socket, &type, sizeof(int)) != sizeof(int)) {
        return -1;
    }
    msg->type = ntohl(type);

    // 接收消息长度
    if (read(socket, &length, sizeof(int)) != sizeof(int)) {
        return -1;
    }
    msg->length = ntohl(length);

    // 检查消息长度是否合法
    if (msg->length < 0 || msg->length > sizeof(msg->data)) {
        return -1;
    }

    // 接收消息数据
    if (read(socket, msg->data, msg->length) != msg->length) {
        return -1;
    }

    return 0;
}